package org.smartercontext.local.servlets;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.smartercontext.contextmger.DTO.BooksDTO;
import org.smartercontext.contextmger.DTO.MovieDTO;
import org.smartercontext.contextmger.DTO.MusicDTO;
import org.smartercontext.contextmger.contextsphere.ContextManagerController;
import org.smartercontext.contextmger.utilities.GetMMB;

import com.hp.hpl.jena.rdf.model.Model;

/**
 * Servlet implementation class ModifyMMBServlet
 */
@WebServlet("/ModifyMMBServlet")
public class ModifyMMBServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ModifyMMBServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
    	if(request.getQueryString()!=null){
    		HttpSession session = request.getSession();
    		String username = session.getAttribute("username").toString();
    		
    		if(username!=null){
    			
    			int prefsQuantity = 0;
    			String queryCopy = request.getQueryString();
    			String nameJAV = "nameJAV";
    			while(queryCopy.indexOf(nameJAV)>-1){
    				queryCopy = queryCopy.substring(queryCopy.indexOf(nameJAV)+nameJAV.length(), queryCopy.length());
    				prefsQuantity++;
    			}
    			
    			GetMMB gmmb = new GetMMB();
    			
    			List<MusicDTO>mudto=new ArrayList<>();
    			List<MovieDTO>modto=new ArrayList<>();
    			List<BooksDTO>bdto = new ArrayList<>();
    			
    			for(int i=0;i<prefsQuantity;i++){
    				
    				String parameterName = nameJAV+i;
    				String parameterType = "typeJAV"+i;
    				String parameternameOld = "nameOJAV"+i;
    				
    				String name = request.getParameter(parameterName);
    				String type = request.getParameter(parameterType);
    				String oldName = request.getParameter(parameternameOld);	
    				if(oldName!=null&&name!=null){
    					if(oldName.equals(name)){
    						if(type.equals("Music")){
    							
    							MusicDTO temp = new MusicDTO();
    							temp.setOldName(name);
    							mudto.add(temp);
    						}else if(type.equals("Movies")){
    							MovieDTO temp = new MovieDTO();
    							temp.setOldName(name);
    							modto.add(temp);
    						}else if(type.equals("Books")){
    							BooksDTO temp = new BooksDTO();
    							temp.setOldName(name);
    							bdto.add(temp);
    						}
    					}else{
    						if(type.equals("Music")){
    							MusicDTO temp = new MusicDTO();
    							temp.setOldName(oldName);
    							temp.setName(name);
    							mudto.add(temp);
    						}else if(type.equals("Movies")){
    							MovieDTO temp = new MovieDTO();
    							temp.setOldName(oldName);
    							temp.setName(name);
    							modto.add(temp);
    						}else if(type.equals("Books")){
    							BooksDTO temp = new BooksDTO();
    							temp.setOldName(oldName);  
    							temp.setName(name);
    							bdto.add(temp);
    						}
    					}
    				}
    				
    				gmmb.setBooksPrefs(bdto);
    				gmmb.setMoviesPrefs(modto);
    				gmmb.setMusicPrefs(mudto);
    				
    				
    				try {
    					ContextManagerController.contextManagerController();
						Model usermodel = ContextManagerController.getUsermodel(username);
						Model usermodelU = gmmb.modifyMMBToUserModel(usermodel, username);
						ContextManagerController.integrateContext(usermodelU, username);
						response.sendRedirect("manageContext_M&M.jsp");
					} catch (Exception e) {
						response.sendRedirect("manageContext_M&M.jsp?Error=2");
//						e.printStackTrace();
					}
    				
    				
    			}
    			
    		}else{
    			response.sendRedirect("login.jsp?Err=1");
    		}
    	}
    	
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

}
